/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import footprintscrollbar.FootprintScrollBar;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import model.Model;
import model.Phrase;
import model.StopWatch;

/**
 *
 * @author mehlicious, karsonbraaten
 */
public class View extends JFrame{

    final static int _preferedX = 800;
    final static int _preferedY = 800;
    
    final static int _locationX = 0;
    final static int _locationY = 0;        
    
    final static int _offset = 3855;
    
    Model model;
    //FootprintScrollBar scroll;
    public FPScrollPane sp;
    public JTextArea textArea;
    
    public JPanel searchPanel;
    public JPanel buttonPanel;
    public JTextArea searchPhrase;
    public JButton startButton;
    public JButton foundButton;
    
    public FootprintScrollBar fpsb;
    
    
    public String _textArea = "";
    /**
     * Timing
     */
StopWatch stopWatch = new StopWatch();
        
    public View(Model m, FootprintScrollBar fpsb){

        model = m;

        this.fpsb = fpsb;
        //fpsb.setMaximum(100);
        
        // text stuff
        textArea = new JTextArea(model.story);
        
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        sp = new FPScrollPane(textArea);
        sp.setVerticalScrollBar(fpsb);
        sp.setPreferredSize(new Dimension(_preferedX - 200, _preferedY));
        sp.setWheelScrollingEnabled(true);
        
        searchPanel = new JPanel();
        
        searchPhrase = new JTextArea("Search phrase will appear here.");
        searchPhrase.setEditable(false);
        searchPhrase.setLineWrap(true);
        searchPhrase.setPreferredSize(new Dimension(200, 200));
        startButton = new JButton("Start");
        //startButton.addActionListener(this);
        foundButton = new JButton("Found");
        foundButton.setEnabled(false);
        //foundButton.addActionListener(this);
        
        searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS));
        searchPanel.setPreferredSize(new Dimension(200, _preferedY));
        searchPanel.add(searchPhrase);
        
        buttonPanel = new JPanel();
        buttonPanel.add(startButton);
        buttonPanel.add(foundButton);
        
        searchPanel.add(buttonPanel);
                        
        this.setPreferredSize(new Dimension(_preferedX, _preferedY));
        this.setMinimumSize(new Dimension(_preferedX, _preferedY));
        this.setLocation(_locationX, _locationY);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        addComponentsToPane(this.getContentPane());

    }
    
    public void setSearchText(Phrase p){
        searchPhrase.setText(p.s);
    }
    
    public void highlightText(Phrase p){
        Highlighter h = textArea.getHighlighter();
        h.removeAllHighlights();
        int pos = model.story.indexOf(p.s, 0);
        try {
            h.addHighlight(pos, pos + p.s.length(), DefaultHighlighter.DefaultPainter);
        } catch (BadLocationException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void getTextArea(){
        try {
            _textArea = textArea.getText(fpsb.getValue(), _offset);
        } catch (BadLocationException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/*
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            startButton.setEnabled(false);
            stopWatch.start();
        }
        if (e.getSource() == foundButton) {
            try {
                BufferedReader read = new BufferedReader(new FileReader("performance.txt"));
                
                String old = "";
                
                String line = "";
                while((line = read.readLine()) != null){
                    
                    old += (line + "\n");
                    System.out.println(old);
                }
                
                old += (Long.toString(stopWatch.getElapsedTimeSecs()));
                       
                System.out.println("IN OLD");
                System.out.println(old);
                                read.close();

                
                PrintWriter out = new PrintWriter(new FileWriter("performance.txt"));


                out.print(old);
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
*/    
    public class FPScrollPane extends JScrollPane{
        public FPScrollPane(JTextArea ta) {
            super(ta);
        }
        public FootprintScrollBar getFootprintScrollBar() {
            return (FootprintScrollBar)getVerticalScrollBar();
        }
    }
    
    private void addComponentsToPane(Container pane) {
        //pane.add(scroll, BorderLayout.EAST);
        pane.add(sp, BorderLayout.WEST);
        pane.add(searchPanel, BorderLayout.EAST);
    }
    
}
